import { View } from "@tarojs/components";
import Taro from "@tarojs/taro";
import "./login.scss";

const Login = () => {
  const addUser = (res: any) => {
    try {
      Taro.cloud.callFunction({
        name: "logins",
        data: {
          $url: "addUser",
          nickName: res.userInfo.nickName,
          avatar: res.userInfo.avatarUrl
        }
      });
    } catch (err) {
      console.log(err);
    }
  };

  const login = () => {

    Taro.getUserProfile({
      desc: "获取用户头像，姓名",
      success: res => {
        // console.log(res);
        Taro.setStorageSync("userInfo", res.userInfo);
        addUser(res);
        Taro.navigateBack();
      },
      fail: () => {
        Taro.showToast({ title: "用户登录失败", icon: "error" });
      }
    });
  };

  return (
    <View className='index'>
      <View></View>
      <View className='btn' onClick={login}>
        微信授权一键登录
      </View>
    </View>
  );
};

export default Login;
